package cn.com.view;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import cn.com.action.BoolBarAction;
import db.SqlServer;


public class MainFrame extends JFrame{
	
	private static MainFrame frame;

	BoolBarAction action = new BoolBarAction(this);

	JPanel panel = new JPanel();

	public JComboBox acc;
	
	public JTextField accname;
	
	public JTextField year;
	
	public JTextField path;

	public static final String	CENTER	= "Center";
	
	private JComboBox imptype;
	
	private JFileChooser fd;  //文件选 择器

	/**
	 * 构造方法
	 * 
	 */
	public MainFrame() {
		super("数据导入");
		// 初始化窗体
			init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		this.setSize(600, 300);// 设置窗体大小
		this.add(createJScrollPane(), MainFrame.CENTER);
		this.setDefaultCloseOperation(2);// 设置窗体关闭模式
		this.setLocationRelativeTo(null);// 窗体居中
		this.setVisible(true);// 窗体可见
		
		this.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	/**
	 * 创建流动面板
	 * @return
	 */
	public JPanel createJScrollPane() {

		JLabel accL = new JLabel("帐套");
		accL.setBounds(30, 30, 80, 30);
		accL.setHorizontalAlignment(SwingConstants.RIGHT);
		
		//账套
		Vector<String> dwVector = SqlServer.getDwVector(); //查询单位代码
		acc = new JComboBox(dwVector);
		//acc = new JTextField();
		acc.setBounds(120,30, 150, 30);
		acc.setMinimumSize(new Dimension(100,100));
		acc.setMaximumRowCount(10);
		acc.setEditable(false);
		
		JLabel accnameL = new JLabel("帐套名称");
		accnameL.setBounds(300, 30, 80, 30);
		accnameL.setHorizontalAlignment(SwingConstants.RIGHT);
		accname = new JTextField();
		accname.setBounds(400,30, 150, 30);
		
		JLabel yearL = new JLabel("年度");
		yearL.setBounds(30, 80, 80, 30);
		yearL.setHorizontalAlignment(SwingConstants.RIGHT);
		Date date=new Date();
		SimpleDateFormat format=new SimpleDateFormat("yyyy");
	    String dateString=format.format(date);
		year = new JTextField(dateString);
		year.setBounds(120,80, 150, 30);
		
		JLabel imptypeL = new JLabel("导入类型");
		imptypeL.setBounds(300, 80, 80, 30);
		imptypeL.setHorizontalAlignment(SwingConstants.RIGHT);
		Vector<String> type = new Vector<String>(); //查询单位代码
		type.add("增加");
		type.add("减少");
		imptype = new JComboBox(type);
		//imptype = new JTextField();
		imptype.setBounds(400,80, 150, 30);
		imptype.setMinimumSize(new Dimension(100,100));
		imptype.setMaximumRowCount(10);
		imptype.setEditable(false);
		
		JLabel pathL = new JLabel("导入文件路径");
		pathL.setBounds(30, 130, 80, 30);
		pathL.setHorizontalAlignment(SwingConstants.RIGHT);
		path = new JTextField();
		path.setBounds(120,130, 380, 30);
		
		fd = new JFileChooser();
		fd.setFileSelectionMode(JFileChooser.FILES_ONLY);  
		
		
		String defind=(String) acc.getSelectedItem();
		if(defind != null && !"".equals(defind)){
			String dwmc=SqlServer.getDate(defind);
			accname.setText(dwmc);
		}

		
		acc.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String itemName = acc.getSelectedItem().toString();
				String dwmc = SqlServer.getDate(itemName); //查询创建时间
				if (dwmc == null || "".equals(dwmc)) {
					accname.setText("");
				} else {
					accname.setText(dwmc);
				}
			}
		});
		
		panel.setLayout(null);
		panel.add(acc);
		panel.add(accL);
		panel.add(accname);
		panel.add(accnameL);
		
		panel.add(yearL);
		panel.add(year);
		panel.add(imptypeL);
		panel.add(imptype);
		
		panel.add(pathL);
		panel.add(path);
		panel.add(fd);

		JButton updataButton = createJbutton("导入");
		updataButton.setBounds(170, 170, 50, 30);

		JButton closeButton = createJbutton("退出");
		closeButton.setBounds(350, 170, 50, 30);
		
		JButton selectBut = createJbutton("选择");
		selectBut.setBounds(500, 130, 50, 30);

		panel.add(updataButton);
		panel.add(closeButton);
		panel.add(selectBut);
		return panel;
	}

	/**
	 * 创建按钮
	 * @param title 标题
	 * @return 返回一个按钮
	 */
	public JButton createJbutton(String title) {
		JButton button = new JButton(title);
		button.addActionListener(action);
		button.setMargin(new Insets(0, 0, 0, 0));
		return button;
	}

	public synchronized static MainFrame open() {
		// TODO Auto-generated method stub
		if(frame == null){
			frame = new MainFrame();
		}
		return frame;
	}


	
	public static MainFrame getFrame() {
		return frame;
	}

	
	public static void setFrame(MainFrame frame) {
		MainFrame.frame = frame;
	}

	
	public BoolBarAction getAction() {
		return action;
	}

	
	public void setAction(BoolBarAction action) {
		this.action = action;
	}

	
	public JPanel getPanel() {
		return panel;
	}

	
	public void setPanel(JPanel panel) {
		this.panel = panel;
	}

	
	public JComboBox getAcc() {
		return acc;
	}

	
	public void setAcc(JComboBox acc) {
		this.acc = acc;
	}

	
	public JTextField getAccname() {
		return accname;
	}

	
	public void setAccname(JTextField accname) {
		this.accname = accname;
	}

	
	public JTextField getYear() {
		return year;
	}

	
	public void setYear(JTextField year) {
		this.year = year;
	}

	
	public JTextField getPath() {
		return path;
	}

	
	public void setPath(JTextField path) {
		this.path = path;
	}

	
	public JComboBox getImptype() {
		return imptype;
	}

	
	public void setImptype(JComboBox imptype) {
		this.imptype = imptype;
	}

	
	public JFileChooser getFd() {
		return fd;
	}

	
	public void setFd(JFileChooser fd) {
		this.fd = fd;
	}

	
	public static String getCENTER() {
		return CENTER;
	}
}
